*** Creates
* "data_YLE_wide.dta" (extended dataset)
* "data_YLE_main.dta" (long dataset)
*** from 
* "data_src_YLE_merged_wide_anom.dta" (wide merged YLE data)
* "data_src_munidata_12_17.dta"

* CHANGE THIS
cd /path/to/directory

clear

use "data_src_YLE_merged_wide_anom.dta"

gen firstdiff = refugees2017 - refugees2012

gen loser = 0
replace loser = 1 if elected2012 == 0

gen spare = 0
replace spare = 1 if elected2012 == 0.5

gen incumbent = 0
replace incumbent = 1 if elected2012 == 1

gen rantwice = 0
replace rantwice = 1 if (!missing(votes2012) & !missing(votes2017))

gen answeredtwice = 0
replace answeredtwice = 1 if (!missing(refugees2012) & !missing(refugees2017))

*** before reshaping save this for future use: 

save "data_YLE_wide.dta", replace

reshape long votes voteshare age elected environment refugees urban population ///
    , i(seqnum) j(year) string

*** merging with covariates ***

destring year, replace

merge m:1 muni year using "data_src_munidata_12_17.dta", force

*** drop not used ones: 

drop betwee_15_64 over_64 birth_surplus nro_of_families nro_households ///
    nro_households nro_private_houses nro_rentals secondary_educ employment ///
    non_commuters share_of_pensioners nro_of_jobs agriculture industry service ///
    culture_educ social_health _merge

rename share_of_foreigners foreigners
rename higher_educ highereduc

rename seqnum CandidateID

replace firstdiff = . if year == 2012

gen treat = 0

replace treat = 1 if (year == 2017 & Treated2015 == 1)

replace incumbent = 0 if year == 2012
replace spare     = 0 if year == 2012
replace loser     = 0 if year == 2012

save "data_YLE_main.dta", replace

*******************************************************************************

*** Replication code for MAIN PAPER ***

clear

use "data_YLE_main.dta"

gen percent_pop_arrivals = Refugeespercapita * 100
replace percent_pop_arrivals = 0 if year == 2012

*** first reversing the scale ***

vreverse refugees, gen(ref_2)

*******************************************************************************

*** Footnote 9

gen rural = 0 
replace rural = 1 if (population < 15000 & urban < 60)

ttest ref_2 if year == 2012 & Pretreated == 0, by(rural)

*******************************************************************************

*** footnote 19:  see further

*******************************************************************************

*** footnote 21 BEGIN

*** Mayors, data taken from "data_src_CONFIDENTIAL_Mayors.xlsx" Excel file ***
*** Total respondents 385. Yes: 194, Don't know: 135, No: 56
*** Calculation of percents:
dis 194/385
* 0.5038961
dis 135/385
* 0.35064935
dis 56/385
* 0.14545455

*** Column "Got treated against will of stance negative, but treatment status 1" -> 17 instances

dis 17/56 
* 0.30357143

*** Column "Got treated and wanted to" if stance positive, and treatment status 1" --> 61

dis 61/194
* 0.31443299

*** footnote 21 END

*******************************************************************************

*** page 17 main text (placebo regression on refugee stance in t0):  see further

*******************************************************************************

*** TABLE 1 BEGIN

*** BINARY, CONT. + Cov + LOG + JUST TREATED + UPPER QUARTILE INTENSITY

*** Note that the default N is all candidates in the VAA, irrespective of how many times they ran.
*** My sample uses those that ran twice. The N is derived as:

sum CandidateID if (!missing(firstdiff) & Pretreated == 0)
* 4310

* model 1 *

xtset CandidateID year
xtreg ref_2 treat i.year if Pretreated == 0, fe cluster(municode)

* model 2 *

xtset CandidateID year
xtreg ref_2 percent_pop_arrivals i.year if Pretreated == 0, fe cluster(municode)

* model 3 *

xtset CandidateID year
xtreg ref_2 percent_pop_arrivals i.year urban foreigners changeinpop swedish highereduc ///
    if Pretreated == 0, fe cluster(municode)

* model 4 *

gen ref = 1 + percent_pop_arrivals

gen logref = log(ref)

xtset CandidateID year
xtreg ref_2 logref i.year if Pretreated == 0, fe cluster(municode)

* model 5 (log + covariates)

xtset CandidateID year
xtreg ref_2 logref urban foreigners changeinpop swedish highereduc i.year ///
    if Pretreated == 0, fe cluster(municode)

* model 6 *

gen treated_above_median = 0 
replace treated_above_median = 1 if (percent_pop_arrivals > 1 & year != 2012)

xtset CandidateID year
xtreg ref_2 treated_above_median i.year urban foreigners changeinpop swedish highereduc ///
    if (Pretreated == 0 & Treated2015 == 1), fe cl(municode)

* to get the N

tab CandidateID if (!missing(firstdiff) & Pretreated == 0 & Treated2015 == 1)
* 2078 

*** TABLE 1 END

*******************************************************************************

*** Figure 1:  Interactions with population and urban density ***

*** for this you need to install the package interflex on Stata ***

/* TO DO IF NEEDED
ssc install interflex
*/

gen logpop = log(population)

interflex ref_2 treat logpop if Pretreated == 0 ///
    , vce(r) xd(density) fe(CandidateID year) cl(municode) yr(-1 1)

gen logurban = log(urban)

interflex ref_2 treat logurban if Pretreated == 0 ///
    , vce(r) xd(density) fe(CandidateID year) cl(municode) yr(-1 1)

*******************************************************************************

*** Figure 2 ***

interflex ref_2 treat changeinpop if Pretreated == 0 ///
    , vce(r) xd(density) fe(CandidateID year) cl(municode) yr(-1 1)
interflex ref_2 treat taxrevenue if Pretreated == 0 ///
    , vce(r) xd(density) fe(CandidateID year) cl(municode) yr(-1 1)

*******************************************************************************

*** Table 2: See separate folder

*******************************************************************************

*** Section Policy implications BEGIN

* treated urban 2017 *
sum ref_2 if (rural == 0 & year == 2017 & Treated2015 == 1 & Pretreated == 0)
* 3.121423 

* treated rural in 2017 *
sum ref_2 if (rural == 1 & year == 2017 & Treated2015 == 1 & Pretreated == 0)
* 3.059701 

* treated rural in 2012
sum ref_2 if (rural == 1 & year == 2012 & Treated2015 == 1 & Pretreated == 0)
* 2.572621

* non-treated urban in 2012
sum ref_2 if (rural == 0 & year == 2012 & Treated2015 == 0 & Pretreated == 0)
* 2.754683

* non-treated rural in 2017
sum ref_2 if (rural == 1 & year == 2017 & Treated2015 == 0 & Pretreated == 0)
* 2.745055

* non-treated urban in 2017
sum ref_2 if (rural == 0 & year == 2017 & Treated2015 == 0 & Pretreated == 0)
* 2.954662

* opening reception center and opting in to the voluntary scheme

clear

use "data_src_Muni_info.dta"

*** policy outcomes

tab tookref if Treated2015 == 1 & Pretreated == 0

tab tookref if Treated2015 == 0 & Pretreated == 0

tab tookref if Treated2015 == 1 & Pretreated == 0 & Urban < 60 & Population < 15000

tab tookref if Treated2015 == 0 & Pretreated == 0 & Urban < 60 & Population < 15000

*** Section Policy implications END

*******************************************************************************

*** footnote 19 BEGIN

drop if year == 2012

sum capacity if capacity != 0 & Pretreated == 0, det

*** footnote 19 END

*******************************************************************************

clear

use "data_src_YLE_merged_wide_anom.dta"

*******************************************************************************

*** page 17 main text (placebo regression on refugee stance in t0) BEGIN

reg refugees2012 Refugeespercapita if Pretreated == 0, cl(municode)

*    1.919273   2.527348     0.76   0.448 

*******************************************************************************
